﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using Portal.Systems.Model;
using Portal.Web.Utility;
using Portal.Web.Model;
using Portal.Utility;

namespace Portal.Systems.Render
{
    public class PageZoneRenderBll : WebPartTemplate
    {
        public override string Id
        {
            get
            {
                return "PageZoneRenderBll";
            }
        }

        public override string Title
        {
            get
            {
                return "Quản lý các vùng trong trang";
            }
        }



        static void CheckPermission(string SiteId)
        {
            try
            {
                string UserId = WebSessionUtility.GetCurrentLoginUser().UserId;
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, SystemPermission.StaticId, SystemPermission.funcManage, SystemPermission.funcManage, UserId, true);
            }
            catch (Exception ex)
            {
                string ReturnData = "<div class=CssAccessDenied>" + ex.Message.ToString() + " <span class=CssBack><a href=\"javascript:history.back(-1);\">Quay lại</a></span></div>\r\n";
                throw new Exception(ReturnData);
            }
        }
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CheckPermission(SiteId);
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string PageId = (string)WebEnvironments.Request(SiteId, "PageId");
                if (string.IsNullOrEmpty(PageId))
                {
                    throw new Exception("Tham số không hợp lệ!");
                }
                PageCls OPage = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreatePageModel(SiteId, PageId);
                if (OPage == null)
                {
                    throw new Exception("Trang đã bị xóa hoặc tham số không hợp lệ!");
                }
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string Html =
                   "<script language=javascript>\r\n" +
                   "    function ClientReading()\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        AjaxOut = Portal.Systems.Render.PageZoneRenderBll.ServerSideReading(SiteId).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divListPageZone').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n" +


                   "    function ClientAdd()\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        document.getElementById('divActionForm').style.display='block';\r\n" +
                   "        document.getElementById('divListForm').style.display='none';\r\n" +
                   "        AjaxOut = Portal.Systems.Render.PageZoneRenderBll.DrawAddForm(SiteId).value;\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('txtZoneAlias').focus();\r\n" +
                   "    }\r\n" +

                   "    function ClientUpdate(PageZoneId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        document.getElementById('divActionForm').style.display='block';\r\n" +
                   "        document.getElementById('divListForm').style.display='none';\r\n" +
                   "        AjaxOut = Portal.Systems.Render.PageZoneRenderBll.DrawUpdateForm(SiteId, PageZoneId).value;\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('txtZoneAlias').focus();\r\n" +
                   "    }\r\n" +

                  

                   "    function ClientBack()\r\n" +
                   "    {\r\n" +
                   "        document.getElementById('divActionForm').style.display='none';\r\n" +
                   "        document.getElementById('divListForm').style.display='block';\r\n" +
                   "    }\r\n" +

                   "    function ClientAddPageZone()\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        PageId = document.getElementById('txtPageId').value;\r\n" +
                   "        ZoneAlias = document.getElementById('txtZoneAlias').value;\r\n" +
                   "        ZoneName = document.getElementById('txtZoneName').value;\r\n" +
                   "        Active= document.getElementById('drpSelectStatus').value;\r\n" +
                   "        SortIndex= document.getElementById('txtSortIndex').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.PageZoneRenderBll.ServerSideAdd(SiteId, PageId, ZoneAlias, ZoneName, Active, SortIndex).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = '';\r\n" +
                   "        document.getElementById('divActionForm').style.display = 'none';\r\n" +
                   "        document.getElementById('divListPageZone').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        ClientBack();\r\n" +
                   "    }\r\n" +


                   "    function ClientDelete(PageZoneId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        PageId = document.getElementById('txtPageId').value;\r\n" +
                   "        if(confirm('Bạn đã chắc chắn xóa chưa?')==false)return;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.PageZoneRenderBll.ServerSideDelete(SiteId, PageId, PageZoneId).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divListPageZone').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n" +

                   "    function ClientSavePageZone(PageZoneId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        PageId = document.getElementById('txtPageId').value;\r\n" +
                   "        ZoneAlias = document.getElementById('txtZoneAlias').value;\r\n" +
                   "        ZoneName = document.getElementById('txtZoneName').value;\r\n" +
                   "        Active = document.getElementById('drpSelectStatus').value;\r\n" +
                   "        SortIndex= document.getElementById('txtSortIndex').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.PageZoneRenderBll.ServerSideSave(SiteId, PageId, PageZoneId, ZoneAlias, ZoneName, Active, SortIndex).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        document.getElementById('divActionForm').innerHTML = '';\r\n" +
                   "        document.getElementById('divActionForm').style.display = 'none';\r\n" +
                   "        document.getElementById('divListPageZone').innerHTML = AjaxOut.HtmlContent;\r\n" +
                   "        ClientBack();\r\n" +
                   "    }\r\n" +
                   "</script>\r\n" +
                   "<div id=divActionForm style=\"display:none;\"></div>\r\n" +
                   "<input type=hidden id=txtPageId value=\"" + PageId + "\">\r\n" +
                   "<div class=FormFunction id=divListForm>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientAdd();\"><img class=image src=\"" + ButtonIconRoot + "/Add.png\"><div>Tạo mới</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('PageMan.aspx','_self');\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "  <h3>Thiết lập vùng cho: "+OPage.PageTitle+"</h3>\r\n"+
                   "  <div id=\"divListPageZone\" class=tabcontent>\r\n" +
                         ServerSideReading(SiteId, PageId).HtmlContent +
                   "  </div>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = "<div class=CssAccessDenied>" + ex.Message.ToString() + "</div>\r\n";
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReading(string SiteId,string PageId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {

                PageZoneCls[] PageZones = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingPageZone(SiteId,PageId, null);
                string Html =
                   "            <table>\r\n" +
                   "                <tr>\r\n" +
                   "                    <th class=thIndex>#</th>\r\n" +
                   "                    <th class=thCode>Mã</th>\r\n" +
                   "                    <th>Tiều đề vùng</th>\r\n" +
                   "                    <th>Trạng thái</th>\r\n" +
                   "                    <th></th>\r\n" +
                   "                    <th></th>\r\n" +
                   "                </tr>\r\n";
                for (int iIndex = 0; iIndex < PageZones.Length; iIndex++)
                {
                    string UpdateButton = "<input type=button class=button value=\"Sửa\" onclick=\"javascript:ClientUpdate('" + PageZones[iIndex].PageZoneId + "');\">";
                    string DeleteButton = "<input type=button class=button value=\"Xóa\" onclick=\"javascript:ClientDelete('" + PageZones[iIndex].PageZoneId + "');\">";
                    string Status = "(Ngưng sử dụng)";
                    if (PageZones[iIndex].Active == 1)
                    {
                        Status = "(Sử dụng)";
                    }
                    Html +=
                       "    <tr>\r\n" +
                       "        <td class=tdIndex>" + (iIndex + 1) + "</td>\r\n" +
                       "        <td class=tdCode>" + PageZones[iIndex].ZoneAlias + "</td>\r\n" +
                       "        <td>" + PageZones[iIndex].ZoneName + "</td>\r\n" +
                       "        <td>" + Status + "</td>\r\n" +
                       "        <td style=\"width:10px;\">" + UpdateButton + "</td>\r\n" +
                       "        <td style=\"width:10px;\">" + DeleteButton + "</td>\r\n" +
                       "   </tr>\r\n";
                }
                Html +=
                   "    </table>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawAddForm(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string SelectStatus = "<select id=drpSelectStatus>\r\n" +
                                      "     <option value=\"0\">Chưa sử dụng</option>\r\n" +
                                      "     <option value=\"1\">Sử dụng</option>\r\n" +
                                      "</select>\r\n";
                string Html =
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientAddPageZone();\"><img class=image src=\"" + ButtonIconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientBack();\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "  <div class=CssFormSaveUpdate>\r\n" +
                   "  <h3>Thêm mới trang web</h3>\r\n" +
                   "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Mã vùng:</td>\r\n" +
                   "        <td class=Col2><input id=txtZoneAlias value=\"\" class=textbox type=textbox></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Tiêu vùng:</td>\r\n" +
                   "        <td class=Col2><input id=txtZoneName class=textbox type=textbox></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Trạng thái:</td>\r\n" +
                   "        <td class=Col2>"+SelectStatus+"</td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Thứ tự:</td>\r\n" +
                   "        <td class=Col2><input id=txtSortIndex value=\"1\"></td>\r\n" +
                   "    </tr>\r\n" +
                   "  </table>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawUpdateForm(string SiteId, string PageZoneId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                PageZoneCls OPageZone = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreatePageZoneModel(SiteId, PageZoneId);
                string SelectStatus = 
                              "<select id=drpSelectStatus>\r\n" +
                              "     <option value=\"0\">Chưa sử dụng</option>\r\n" +
                              "     <option value=\"1\">Sử dụng</option>\r\n" +
                              "</select>\r\n";
                if (OPageZone.Active == 1)
                {
                    SelectStatus =
                              "<select id=drpSelectStatus>\r\n" +
                              "     <option value=\"0\">Chưa sử dụng</option>\r\n" +
                              "     <option selected value=\"1\">Sử dụng</option>\r\n" +
                              "</select>\r\n";
                }
                

                string Html =
                   " <div class=DatabaseFunction>\r\n" +
                   "    <ul>\r\n" +
                   "        <li onclick=\"javascript:ClientSavePageZone('" + PageZoneId + "');\"><img class=image src=\"" + ButtonIconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                   "        <li onclick=\"javascript:ClientBack();\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "    </ul>\r\n" +
                   "  </div>\r\n" +
                   "  <div class=CssFormSaveUpdate>\r\n" +
                   "  <h3>Sửa thông số trang web</h3>\r\n" +
                   "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Mã vùng:</td>\r\n" +
                   "        <td class=Col2><input readonly id=txtZoneAlias class=textbox type=textbox value=\"" + OPageZone.ZoneAlias + "\"></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Tên vùng:</td>\r\n" +
                   "        <td class=Col2><input id=txtZoneName class=textbox type=textbox value=\"" + OPageZone.ZoneName + "\"></td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Trạng thái:</td>\r\n" +
                   "        <td class=Col2>"+SelectStatus+"</td>\r\n" +
                   "    </tr>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Thứ tự:</td>\r\n" +
                   "        <td class=Col2><input id=txtSortIndex value=\""+OPageZone.SortIndex+"\"></td>\r\n" +
                   "    </tr>\r\n" +
                   "  </table>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideAdd(
            string SiteId, 
            string PageId,
            string ZoneAlias, 
            string ZoneName,
            string Active,
            string SortIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                if (string.IsNullOrEmpty(ZoneAlias)) throw new Exception("Chưa nhập mã vùng!");
                if (string.IsNullOrEmpty(ZoneName)) throw new Exception("Chưa nhập tiêu đề vùng!");
                string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
                PageZoneCls OPageZone = new PageZoneCls();
                OPageZone.PageId = PageId;
                OPageZone.ZoneAlias = ZoneAlias;
                OPageZone.ZoneName = ZoneName;
                OPageZone.Active = int.Parse(Active);
                OPageZone.SortIndex = int.Parse(SortIndex);
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddPageZone(SiteId, OPageZone);
                OAjaxOut.HtmlContent = ServerSideReading(SiteId,PageId).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideSave(
            string SiteId, 
            string PageId,
            string PageZoneId,
            string ZoneAlias, 
            string ZoneName,
            string Active,
            string SortIndex)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                if (string.IsNullOrEmpty(ZoneAlias)) throw new Exception("Chưa nhập mã trang web!");
                if (string.IsNullOrEmpty(ZoneName)) throw new Exception("Chưa nhập tiêu đề trang web!");

                PageZoneCls OPageZone = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreatePageZoneModel(SiteId, PageZoneId);
                OPageZone.ZoneAlias = ZoneAlias;
                OPageZone.ZoneName = ZoneName;
                OPageZone.Active = int.Parse(Active);
                OPageZone.SortIndex = int.Parse(SortIndex);
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).SavePageZone(SiteId, PageZoneId, OPageZone);
                OAjaxOut.HtmlContent = ServerSideReading(SiteId,PageId).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDelete(string SiteId, string PageId,string PageZoneId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).DeletePageZone(SiteId, PageZoneId);
                OAjaxOut.HtmlContent = ServerSideReading(SiteId, PageId).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }
    }
}
